package fileAndIO;

import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;

// 字节流
public class Test2 {
    @Test
    public void test() throws IOException {
        // 文件写入
        OutputStream outputStream = new FileOutputStream("C:\\Users\\83726\\ccc\\aa.txt", true);
        outputStream.write("YUYUDEQIU\n".getBytes(StandardCharsets.UTF_8));
        outputStream.close();
    }

    @Test
    public void test2() throws IOException {
        // 文件读取
        InputStream inputStream = new FileInputStream("C:\\Users\\83726\\ccc\\aa.txt");
        int b;
        while ((b = inputStream.read()) != -1) {
            System.out.print((char) b);
        }
        inputStream.close();
    }

    @Test
    public void test3() throws IOException {
        // 文件读取 用字节数组
        InputStream inputStream = new FileInputStream("C:\\Users\\83726\\ccc\\aa.txt");
        int len;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            System.out.print(new String(bytes, 0, len));
        }
        inputStream.close();
    }

    @Test
    public void test4() throws IOException {
        // 复制过程
        InputStream inputStream = new FileInputStream("C:\\Users\\83726\\Desktop\\ccc.jpg");
        OutputStream outputStream = new FileOutputStream("C:\\Users\\83726\\Desktop\\vvv.jpg");
        int len;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        inputStream.close();
        outputStream.close();
    }

}
